package com.loongtech.bi.action.authManagement;

import com.loongtech.bi.dao.CommonDao;
import com.loongtech.bi.entity.POJO.PojoProjectRole;
import com.loongtech.bi.entity.POJO.PojoUserProjectRole;
import com.loongtech.bi.entity.system.EntitySysRole;
import com.loongtech.bi.entity.system.EntitySysUserRole;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysProjectManager;
import com.loongtech.bi.manager.system.SysUserProjectRoleManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: yuri
 * Description: 用户项目角色管理
 * Date: 16:03 2017/5/4
 */
@RestController
@Scope("prototype")
@RequestMapping("/user_project_role")
public class SysUserProjectRoleAction implements Serializable {
    private static final long serialVersionUID = 1L;
    @Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private SysUserProjectRoleManager sysUserProjectRoleManager;
    @Resource
    private SysProjectManager sysProjectManager;
    @Resource
    private CommonDao commonDao;

    @RequestMapping(value = "projects.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> projectlist(@RequestParam Integer userId) {
        List<PojoProjectRole> list;
        try {
            String sql = "select r.projectId, r.roleId, e.name from system_user_role r , system_role e where r.roleId = e.id and r.userId = ?";
            list = commonDao.queryObjList(sql, new Object[]{userId},PojoProjectRole.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        Map<Integer, ArrayList<EntitySysRole>> map = new HashMap<>();
        if (list != null && list.size() > 0) {
            for (PojoProjectRole aList : list) {
                ArrayList<EntitySysRole> temp = map.get(aList.getProjectId());
                if (temp == null) {
                    temp = new ArrayList<>();
                    map.put(aList.getProjectId(), temp);
                }
                EntitySysRole e = new EntitySysRole();
                e.setId(aList.getRoleId());
                e.setName(aList.getName());
                temp.add(e);
            }
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), map);
    }

    @RequestMapping(value = "roles.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> rolelist(@RequestParam Integer userId, @RequestParam Integer projectId) {
        List<EntitySysRole> list;
        try {
            list = sysUserProjectRoleManager.findByQuery("select distinct p.id, p.name from EntitySysUserRole r, EntitySysRole p where r.roleId = p.id and r.userId = ? and r.projectId = ? order by p.id asc", userId, projectId);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), list);
    }

    @RequestMapping(value = "modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> modify(@RequestBody PojoUserProjectRole pojo) {
        try {
            sysUserProjectRoleManager.excuteUpdateByHql("delete from EntitySysUserRole where userId = ? and projectId = ?", pojo.getUserId(), pojo.getProjectId());
            for (Integer item : pojo.getRoleId()) {
                EntitySysUserRole entity = new EntitySysUserRole();
                entity.setUserId(pojo.getUserId());
                entity.setProjectId(pojo.getProjectId());
                entity.setRoleId(item);
                sysUserProjectRoleManager.persist(entity);
            }
            sysLogManager.addLog(session.getUserName(), "增加用户项目角色关联信息成功.");
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }
}